package top.wineye.spring.dependency.lookup;


import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import top.wineye.spring.dependency.pojo.Biz;

/**
 * @see org.springframework.beans.factory.HierarchicalBeanFactory
 *
 * 从 接口关系看 ApplicationContext 是 HierarchicalBeanFactory 的子接口
 * 同时 HierarchicalBeanFactory -> ConfigurableBeanFactory -> ConfigurableListBeanFactory
 *
 */
public class HierarchicalBeanFactoryLookup {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(HierarchicalBeanFactoryLookup.class);


        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        System.out.println("current beanFactory's papa: " + beanFactory.getParentBeanFactory());

        // setting parent BeanFactory
        DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(parentBeanFactory);
        xmlReader.loadBeanDefinitions("hierarchical-bean-factory-context.xml");
        beanFactory.setParentBeanFactory(parentBeanFactory);

        Biz aBiz = context.getBean("aBiz", Biz.class);
        System.out.println("a-biz = " + aBiz);

        context.close();
    }

    @Bean
    public Biz aBiz() {
        Biz biz = new Biz();
        biz.setBiz("@Bean biz");
        return biz;
    }
}
